// subpages/pages/cancelNFCBind/cancelNFCBind.js
//获取应用实例
var util = require('../../utils/util.js')
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        success:0,
        loadding : 0,
        year:"",
        input:"input",
        itemArr:[],
        realName:"",
        phone:"",
        position:"",
        provinces: '',    //省份
    city: '', //城市
    county: '', 
    subInfo:[],  // 区县
    },
    
    // 定位
  loadInfo: function () {
    var that = this
    wx.getLocation({
     type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标 
     success: function (res) {
     // success 
     var longitude = res.longitude
     var latitude = res.latitude
     that.loadCity(longitude, latitude)
     },
     fail: function () {
     // fail 
     },
     complete: function () {
     // complete 
     }
    })
    },
 loadCity: function (longitude, latitude) {
      console.log(longitude,'11111111');
      console.log(latitude,'2222222222');
    var that = this
    wx.request({
     url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=ERO0GRzqa6iUrGRIGHd2Bzu9ODw6bkZi&output=json&coordtype=gcj02&location='+ latitude +','+ longitude +'',
//这里的ak 是去百度地图api获取的需要自己登陆获取一下 地址：https://lbsyun.baidu.com/index.php?title=wxjsapi
     data: {},
     header: {
     'Content-Type': 'application/json'
     },
     success: function (res) {
     console.log(res);
     var provinces = res.data.result.addressComponent.province; // 省
     var city = res.data.result.addressComponent.city;          // 市
     var county = res.data.result.addressComponent.district;  // 区/县
     console.log("城市为" +provinces + city + county)
     that.setData({ city: city,provinces:provinces,county:county });  //赋值
     },
     fail: function () {
     // fail 
     },
     complete: function () {
     // complete 
     }
    })
    },

   
    onLoad: function (options) {
        let year="";
        let month="";
        let day="";
        var d=new Date();
       // let baseurl='https://www.fastmock.site/mock/1a7f5b2d0b12109d0196a52364016a1c/epidemic';
        year=d.getFullYear();
        month=d.getMonth()+1;
        day=d.getDate();
        this.setData({
            year: year,
            month:month,
            day:day,
            success:0,
        })
        wx.request({
            url: app.dailyUrl + '/title/selectTitleByDepartment',
            method: 'GET',
            data: {
                departmentId:app.globalData.departmentId
             },
            dataType: 'json',
            success: res => {
                console.log("Arr2", res.data);
              let itemArr=res.data;
              this.setData({
                  itemArr:itemArr
              })
            }
            
          });
          wx.request({
            url: app.baseUrl + '/WeChat/AccountManagement/getUserByWeChatId',
            method: 'POST',
            data: {
              weChatId: app.globalData.wechatId
            },
            dataType: 'json',
            success: res => {
               console.log(res.data[0].realName)
               this.setData({
                realName:res.data[0].realName,
                phone:res.data[0].phone
               })
              
            }
          });
          
           this.loadInfo();
           
         
        
        
    },
    formSubmit: util.throttle(function(e) {
            // console.log("test", e);
            // console.log(this.data.itemArr)
            let userInfo={};
            let Info=[];
            userInfo={
                "userId":app.globalData.userId,
                "username":app.globalData.username,
                "phone":app.globalData.phone,
                "realname":app.globalData.realname
            }
            let subInfo={};
           
            let waringInfo = "";
            let flag=0;
            if(e.detail.value.name==""||e.detail.value.phone==""||e.detail.value.position==""){
                flag=1;
            }
            let vals=e.detail.value;
            for(var p in vals){
                // console.log("++", p);
                // console.log("+++", vals[p]);
                var m=p.split(",");
                // console.log("rs", m)
                if(m[0]=="input"){
                    Info.push({
                        title:m[1],
                        content:vals[p],
                        level:"0"
                    })
                }
                else if(m[0]=="radio"){
                    var n=vals[p].split(",");
                    Info.push({
                        title:m[1],
                        content:n[0],
                        level:n[1]
                    })
                }
                // console.log("+++", this.data.itemArr.data.titleList);
                if(vals[p]==""||vals[p]==null){
                    flag=1;
                }
            } 
            subInfo = {
                "userInfo":userInfo,
                "info":Info
            }
            this.setData({
                subInfo:subInfo
            })
            console.log("data", this.data.subInfo);
           if(flag==1){
            waringInfo="填写表单不完全"
           }
           if (waringInfo != "") {
            wx.showToast({
              title: waringInfo,
              image: "/images/warning.png"
            })
          }
          else{
              wx.request({
                url: app.dailyUrl + '/record/insertRecord',
            method: 'POST',
            data: this.data.subInfo,
            dataType: 'json',
            success: res => {
                console.log("Arr2", res.data);
                this.setData({
                    success:1
                })
            }
              })
          }
            
          

    }),
    

   
})